/**
 *====================================================
 * 文件名称: PickupBulkstockServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.project.entity.PickupApplicationEntity;
import com.yuexunit.finance.project.entity.PickupBulkstockEntity;
import com.yuexunit.finance.project.entity.lexicon.ApplicationStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;

/**
 * @ClassName: PickupBulkstockServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(PickupBulkstockService.class)
public class PickupBulkstockServiceImpl implements PickupBulkstockService {

	@Override
	public Long createPickupBulkstock(PickupBulkstockEntity entity) {
		entity.setPickup(BigDecimal.ZERO);
		PersistUtil.insert(entity);
		return entity.getPickupBulkstockId();
	}

	@Override
	public void updatePickupBulkstock(PickupBulkstockEntity entity) {
		PickupBulkstockEntity persist = inquirePickupBulkstock(entity.getPickupBulkstockId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public void renovatePickupBulkstock(PickupBulkstockEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public void deletePickupBulkstock(Long pickupBulkstockId) {
		PersistUtil.delete(PickupBulkstockEntity.class, pickupBulkstockId);
	}

	public PickupBulkstockEntity inquirePickupBulkstock(Long pickupBulkstockId) {
		return PersistUtil.get(PickupBulkstockEntity.class, pickupBulkstockId);
	}

	@Override
	public List<PickupBulkstockEntity> inquirePickupBulkstockList(Long pickupId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupBulkstockEntity.class);
		criteria.add(Restrictions.eq("pickupId", pickupId));
		return PersistUtil.get(criteria);
	}

	@Override
	public PickupBulkstockEntity inquirePickupBulkstock(Long pickupId, Long bulkstockId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupBulkstockEntity.class);
		criteria.add(Restrictions.eq("pickupId", pickupId));
		criteria.add(Restrictions.eq("bulkstockId", bulkstockId));
		List<PickupBulkstockEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public Map<Long, Integer> inquirePickupFinanceBulkstock(Long financeId) {
		Map<Long, Integer> result = new HashMap<>();
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		criteria.add(Restrictions.in("applicationState", new ApplicationStateEnum[] { ApplicationStateEnum.NOT_APPROVE, ApplicationStateEnum.AGREE, ApplicationStateEnum.RECEIPT, ApplicationStateEnum.RELEASE }));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("pickupId")));
		List<Long> pickupIds = PersistUtil.get(criteria);
		if (pickupIds.isEmpty()) {
			return result;
		}
		criteria = DetachedCriteria.forClass(PickupBulkstockEntity.class);
		criteria.add(Restrictions.in("pickupId", pickupIds));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("bulkstockId")).add(Projections.sum("quantity")));
		List<Object> bulkstocks = PersistUtil.get(criteria);
		for (Object o : bulkstocks) {
			Object[] m = (Object[]) o;
			result.put(Long.parseLong(m[0].toString()), Integer.parseInt(m[1].toString()));
		}
		return result;
	}

}
